<?php

namespace app\util;

use think\Log;
use think\Request;

/*
 * 微信支付
 */

class WxPay
{
    //appid
    const APPID = 'wxf4d3b027af13fec6';

    //商户号
    const MCH_ID = '1326020601';

    //key
    const WX_KEY = 'zxcvbnmlkjhgfdsa12345678iuytrewq';

    //微信请求地址
    const CURL_URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

    //回调地址
    const NOTIFY_URL = 'http://app.dwkkm.com/kkmtest/public/index/pay/wxnotify';
    
    //回调地址（优享）
    const USHARE_NOTIFY_URL = 'http://app.dwkkm.com/kkmtest/public/index/pay/usharewxnotify';
    
    //微信退款请求地址
    const REFUND_CURL_URL = 'https://api.mch.weixin.qq.com/secapi/pay/refund';

    //退款回调地址
    const REFUND_NOTIFY_URL = 'http://app.dwkkm.com/kkmtest/public/index/pay/wxrefundnotify';
    
    //退款回调地址(小程序)
    const XCX_REFUND_NOTIFY_URL = 'http://app.dwkkm.com/kkmtest/public/index/pay/xcxwxrefundnotify';

    //微信退款查询接口
    const CURL_REFUNDQUERY_URL='https://api.mch.weixin.qq.com/pay/refundquery';

    /*
     * 微信支付
     */
    public static function doPay($out_refund_no, $total_fee, $params = [])
    {
        $ip = Request::instance()->ip();

        $data['appid'] = self::APPID;
        $data['mch_id'] = self::MCH_ID;
        $data['nonce_str'] = self::generateNonceStr();
        $data['sign_type'] = 'MD5';
        $data['body'] = $out_refund_no;
        $data['attach'] = $out_refund_no;
        $data['out_trade_no'] = $out_refund_no;
        $data['fee_type'] = 'CNY';
        $data['total_fee'] = $total_fee * 100;  //金额为分
        $data['spbill_create_ip'] = $ip;
        $data['notify_url'] = self::NOTIFY_URL;
        $data['trade_type'] = 'APP';
        if (count($params) > 0) {
            //限时抢购设置交易起始时间和交易结束时间
            $data['time_start'] = date('YmdHis', $params['time_start']);
            $data['time_expire'] = date('YmdHis', $params['time_start'] + 900);
        }
        $data['sign'] = self::sign($data);

        $xml = self::arrayToXml($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::CURL_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $result = curl_exec($ch);

        if (!curl_errno($ch)) {
            $responseData = self::xmlToArray($result);

            if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
                /*
                $payData['appid'] = self::APPID;
                $payData['partnerid'] = self::MCH_ID;
                $payData['prepayid'] = $responseData['prepay_id'];
                $payData['package'] = 'Sign=WXPay';
                $payData['noncestr'] = self::generateNonceStr();
                $payData['timestamp'] = time();
                $payData['sign'] = self::sign($payData);

                return [
                    'code' => 0,
                    'data' => $payData,
                ];
                */

                return [
                    'code' => 0,
                    'data' => $responseData,
                ];
            } else {
                return [
                    'code' => 1,
                    'data' => $responseData['return_msg'],
                ];
            }
        } else {
            $error = curl_error($ch);
            return [
                'code' => 1,
                'data' => $error,
            ];
        }
        curl_close($ch);
    }
    
    /*
     * 微信支付(优享活动)
     */
    public static function usharedoPay($out_refund_no, $total_fee, $params = [])
    {
        $ip = Request::instance()->ip();

        $data['appid'] = self::APPID;
        $data['mch_id'] = self::MCH_ID;
        $data['nonce_str'] = self::generateNonceStr();
        $data['sign_type'] = 'MD5';
        $data['body'] = $out_refund_no;
        $data['attach'] = $out_refund_no;
        $data['out_trade_no'] = $out_refund_no;
        $data['fee_type'] = 'CNY';
        $data['total_fee'] = $total_fee * 100;  //金额为分
        $data['spbill_create_ip'] = $ip;
        $data['notify_url'] = self::USHARE_NOTIFY_URL;
        $data['trade_type'] = 'APP';
        if (count($params) > 0) {
            //限时抢购设置交易起始时间和交易结束时间
            $data['time_start'] = date('YmdHis', $params['time_start']);
            $data['time_expire'] = date('YmdHis', $params['time_start'] + 900);
        }
        $data['sign'] = self::sign($data);

        $xml = self::arrayToXml($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::CURL_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $result = curl_exec($ch);

        if (!curl_errno($ch)) {
            $responseData = self::xmlToArray($result);

            if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
                /*
                $payData['appid'] = self::APPID;
                $payData['partnerid'] = self::MCH_ID;
                $payData['prepayid'] = $responseData['prepay_id'];
                $payData['package'] = 'Sign=WXPay';
                $payData['noncestr'] = self::generateNonceStr();
                $payData['timestamp'] = time();
                $payData['sign'] = self::sign($payData);

                return [
                    'code' => 0,
                    'data' => $payData,
                ];
                */

                return [
                    'code' => 0,
                    'data' => $responseData,
                ];
            } else {
                return [
                    'code' => 1,
                    'data' => $responseData['return_msg'],
                ];
            }
        } else {
            $error = curl_error($ch);
            return [
                'code' => 1,
                'data' => $error,
            ];
        }
        curl_close($ch);
    }

    /*
     * 生成nonce_str
     */
    public static function generateNonceStr()
    {
        $string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $length = strlen($string);

        $rand = '';
        for ($i = 0; $i < 32; $i++) {
            $start = mt_rand(0, $length - 1);
            $char = substr($string, $start, 1);

            $rand = $rand . $char;
        }

        return $rand;
    }

    /*
     * 生成sign
     */
    public static function sign($data)
    {
        $string = '';

        ksort($data);
        foreach ($data as $key => $value) {
            $string = $string . $key . '=' . $value . '&';
        }

        $string = $string . 'key=' . self::WX_KEY;
        $sign = strtoupper(md5($string));

        return $sign;
    }
    
    /*
     * 生成sign
     */
    public static function xcxsign($data)
    {
        $string = '';

        ksort($data);
        foreach ($data as $key => $value) {
            $string = $string . $key . '=' . $value . '&';
        }

        $string = $string . 'key=iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5';
        $sign = strtoupper(md5($string));

        return $sign;
    }

    /*
     * 数组转xml
     */
    public static function arrayToXml($data)
    {
        $xml = '<xml>';

        foreach ($data as $key => $value) {
            if (is_numeric($value)) {
                $xml = $xml . "<" . $key . ">" . $value . "</" . $key . ">";
            } else {
                $xml = $xml . "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
            }

            //$xml = $xml . '<' . $key . '>' . $value . '</' . $key . '>';
        }
        $xml = $xml . '</xml>';

        return $xml;
    }

    /*
     * xml转数组
     */
    public static function xmlToArray($xml)
    {
        return (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    }
    
    /*
     * 查账对账（APP微信）
     * author wxf 
     * date 2017-09-16   
     */

    public function AppWxAudit($out_refund_no) {
            //这个php随机函数能生成XC*=z~7L这样的字符串，够随机！
            $arr = array_merge(range('a', 'z'), range('A', 'Z'));
            $str = '';
            $arr_len = count($arr);
            for ($i = 0; $i < 32; $i++) {
                $rand = mt_rand(0, $arr_len - 1); //使用 Mersenne Twister 算法返回随机整数。
                $str.=$arr[$rand];
            }
            //签名
            //$out_refund_no = $all['out_refund_no']; //订单号
            //$transaction_id = $all['transaction_id']; //微信订单号
            //$out_refund_no = '146476336767';
            $filter = array('appid', 'mch_id', 'nonce_str', "out_trade_no");
            $base = array(
                'appid' => "wxf4d3b027af13fec6", //应用idwxf4d3b027af13fec6
                'mch_id' => '1326020601', //商户号
                'nonce_str' => $str, //随机字符串
                "out_trade_no" => $out_refund_no, //商户订单号
            );
            $array = array();
            foreach ($base as $k => $v) {
                if (in_array($k, $filter)) {
                    $array[$k] = $v;
                }
            }
            ksort($array); //按照键名对关联数组进行升序排序：
            reset($array); //把内部指针移动到数组的首个元素
            $signPars = '';
            foreach ($array as $k => $v) {
                $signPars .= $k . "=" . $v . "&";
            }
            $sign = strtoupper(md5($signPars . "key=zxcvbnmlkjhgfdsa12345678iuytrewq")); //签名  把字符串转换为大写
            //传值
            $array = array(
                'appid' => "wxf4d3b027af13fec6", //应用id
                'mch_id' => '1326020601', //商户号
                'nonce_str' => $str, //随机字符串
                "out_trade_no" => $out_refund_no, //商户订单号
                "sign" => $sign, //签名
            );

            $url = "https://api.mch.weixin.qq.com/pay/orderquery";
//        $xml = arrayToXml($array);

            $xml = "<xml>";
            foreach ($array as $key => $val) {
                if (is_numeric($val)) {
                    $xml.="<" . $key . ">" . $val . "</" . $key . ">";
                } else {
                    $xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                    //$xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                }
            }
            $xml.="</xml>";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            $data = curl_exec($ch);
            curl_close($ch);
            return $array_data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

//            if ($array_data['return_code'] == 'SUCCESS' && $array_data['result_code'] == 'SUCCESS') {
//                return $this->ajaxReturn(0, "查询成功", $array_data);
//            } else {
//                return $this->ajaxReturn(1, "查询失败", null);
//            }
    }

    /*
     * 查账对账（H5微信）
     * author wxf 
     * date 2017-09-16   
     */

    public function H5WxAudit($out_refund_no,$transaction_id=false) {
            //这个php随机函数能生成XC*=z~7L这样的字符串，够随机！
            $arr = array_merge(range('a', 'z'), range('A', 'Z'));
            $str = '';
            $arr_len = count($arr);
            for ($i = 0; $i < 32; $i++) {
                $rand = mt_rand(0, $arr_len - 1); //使用 Mersenne Twister 算法返回随机整数。
                $str.=$arr[$rand];
            }
            //签名
            //$out_refund_no = $all['out_refund_no']; //订单号
            //$transaction_id = $all['transaction_id']; //微信订单号
            //$out_refund_no = '146476336767';
            if($out_refund_no){
               $filter = array('appid', 'mch_id', 'nonce_str', "out_trade_no");
                $base = array(
                    'appid' => "wx086510b9304c71a1", //应用idwxf4d3b027af13fec6
                    'mch_id' => '1355332802', //商户号
                    'nonce_str' => $str, //随机字符串
                    "out_trade_no" => $out_refund_no, //商户订单号
                );
                $array = array();
                foreach ($base as $k => $v) {
                    if (in_array($k, $filter)) {
                        $array[$k] = $v;
                    }
                }
                ksort($array); //按照键名对关联数组进行升序排序：
                reset($array); //把内部指针移动到数组的首个元素
                $signPars = '';
                foreach ($array as $k => $v) {
                    $signPars .= $k . "=" . $v . "&";
                }
                $sign = strtoupper(md5($signPars . "key=iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5")); //签名  把字符串转换为大写
                //传值
                $array = array(
                    'appid' => "wx086510b9304c71a1", //应用id
                    'mch_id' => '1355332802', //商户号
                    'nonce_str' => $str, //随机字符串
                    "out_trade_no" => $out_refund_no, //商户订单号
                    "sign" => $sign, //签名
                ); 
            }elseif ($transaction_id) {
                $filter = array('appid', 'mch_id', 'nonce_str',"transaction_id");
                $base = array(
                    'appid' => "wx086510b9304c71a1", //应用idwxf4d3b027af13fec6
                    'mch_id' => '1355332802', //商户号
                    'nonce_str' => $str, //随机字符串
                    "transaction_id" => $transaction_id,//微信订单号
                );
                $array = array();
                foreach ($base as $k => $v) {
                    if (in_array($k, $filter)) {
                        $array[$k] = $v;
                    }
                }
                ksort($array); //按照键名对关联数组进行升序排序：
                reset($array); //把内部指针移动到数组的首个元素
                $signPars = '';
                foreach ($array as $k => $v) {
                    $signPars .= $k . "=" . $v . "&";
                }
                $sign = strtoupper(md5($signPars . "key=iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5")); //签名  把字符串转换为大写
                //传值
                $array = array(
                    'appid' => "wx086510b9304c71a1", //应用id
                    'mch_id' => '1355332802', //商户号
                    'nonce_str' => $str, //随机字符串
                    "transaction_id" => $transaction_id,//微信订单号
                    "sign" => $sign, //签名
                ); 
            }
           
            $url = "https://api.mch.weixin.qq.com/pay/orderquery";
//        $xml = arrayToXml($array);

            $xml = "<xml>";
            foreach ($array as $key => $val) {
                if (is_numeric($val)) {
                    $xml.="<" . $key . ">" . $val . "</" . $key . ">";
                } else {
                    $xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                    //$xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                }
            }
            $xml.="</xml>";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            $data = curl_exec($ch);
            curl_close($ch);
            return $array_data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

//            if ($array_data['return_code'] == 'SUCCESS' && $array_data['result_code'] == 'SUCCESS') {
//                return $this->ajaxReturn(0, "查询成功", $array_data);
//            } else {
//                return $this->ajaxReturn(1, "查询失败", null);
//            }
    }
    
    /*
     * 查账对账（小程序微信）
     * author wxf 
     * date 2017-09-16   
     */

    public function XcxWxAudit($out_refund_no) {
            //这个php随机函数能生成XC*=z~7L这样的字符串，够随机！
            $arr = array_merge(range('a', 'z'), range('A', 'Z'));
            $str = '';
            $arr_len = count($arr);
            for ($i = 0; $i < 32; $i++) {
                $rand = mt_rand(0, $arr_len - 1); //使用 Mersenne Twister 算法返回随机整数。
                $str.=$arr[$rand];
            }
            //签名
            //$out_refund_no = $all['out_refund_no']; //订单号
            //$transaction_id = $all['transaction_id']; //微信订单号
            //$out_refund_no = '146476336767';
            $filter = array('appid', 'mch_id', 'nonce_str', "out_trade_no");
            $base = array(
                'appid' => "wxf03b6b5aaedfa785", //应用idwxf4d3b027af13fec6
                'mch_id' => '1355332802', //商户号
                'nonce_str' => $str, //随机字符串
                "out_trade_no" => $out_refund_no, //商户订单号
            );
            $array = array();
            foreach ($base as $k => $v) {
                if (in_array($k, $filter)) {
                    $array[$k] = $v;
                }
            }
            ksort($array); //按照键名对关联数组进行升序排序：
            reset($array); //把内部指针移动到数组的首个元素
            $signPars = '';
            foreach ($array as $k => $v) {
                $signPars .= $k . "=" . $v . "&";
            }
            $sign = strtoupper(md5($signPars . "key=iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5")); //签名  把字符串转换为大写
            //传值
            $array = array(
                'appid' => "wxf03b6b5aaedfa785", //应用id
                'mch_id' => '1355332802', //商户号
                'nonce_str' => $str, //随机字符串
                "out_trade_no" => $out_refund_no, //商户订单号
                "sign" => $sign, //签名
            );

            $url = "https://api.mch.weixin.qq.com/pay/orderquery";
//        $xml = arrayToXml($array);

            $xml = "<xml>";
            foreach ($array as $key => $val) {
                if (is_numeric($val)) {
                    $xml.="<" . $key . ">" . $val . "</" . $key . ">";
                } else {
                    $xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                    //$xml.="<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                }
            }
            $xml.="</xml>";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            $data = curl_exec($ch);
            curl_close($ch);
            return $array_data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

//            if ($array_data['return_code'] == 'SUCCESS' && $array_data['result_code'] == 'SUCCESS') {
//                return $this->ajaxReturn(0, "查询成功", $array_data);
//            } else {
//                return $this->ajaxReturn(1, "查询失败", null);
//            }
    }

    /*
     * 微信退款
     */
    public static function WxRefund($out_refund_no, $total_fee,$refund_fee)
    {

//        $sslcert_path = dirname(realpath(__FILE__)) . "\\cert\\apiclient_cert.pem";
//        $sslkey_path = dirname(realpath(__FILE__)) . "\\cert\\apiclient_key.pem";
        
        $sslcert_path = ROOT_PATH . "application/util/cert/apiclient_cert.pem";
        $sslkey_path = ROOT_PATH . "application/util/cert/apiclient_key.pem";

        $data['appid'] = self::APPID;
        $data['mch_id'] = self::MCH_ID;
        $data['nonce_str'] = self::generateNonceStr();
        $data['sign_type'] = 'MD5';
        $data['out_trade_no'] = $out_refund_no;
        $data['out_refund_no'] = $out_refund_no;
        $data['total_fee'] = $total_fee * 100;  //订单金额为分
        $data['refund_fee'] = $refund_fee * 100; //退款金额为分
        $data['refund_fee_type'] = 'CNY';
        //$data['refund_account'] = 'REFUND_SOURCE_RECHARGE_FUNDS';
        $data['notify_url'] = self::REFUND_NOTIFY_URL;
        $data['sign'] = self::sign($data);

        $xml = self::arrayToXml($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::REFUND_CURL_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //ssl证书
        curl_setopt($ch,CURLOPT_SSLCERT, $sslcert_path);
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, $sslkey_path);

        $result = curl_exec($ch);

        if (!curl_errno($ch)) {
            $responseData = self::xmlToArray($result);
            if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
                return [
                    'code' => 0,
                    'data' => $responseData,
                ];
            } else {
                return [
                    'code' => 1,
                    'data' => $responseData,
                ];
            }
        } else {
            $error = curl_error($ch);
            return [
                'code' => 2,
                'data' => $error,
            ];
        }
        curl_close($ch);
    }
    
    /*
     * 微信退款查询
     */
    public static function findrefund($out_refund_no, $params = [])
    {
        $ip = Request::instance()->ip();

        $data['appid'] = self::APPID;
        $data['mch_id'] = self::MCH_ID;
        $data['nonce_str'] = self::generateNonceStr();
        $data['out_trade_no'] = $out_refund_no;
        $data['sign'] = self::sign($data);

        $xml = self::arrayToXml($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::CURL_REFUNDQUERY_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $result = curl_exec($ch);

        if (!curl_errno($ch)) {
            $responseData = self::xmlToArray($result);

            if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS' && $responseData["refund_status_0"]=='PROCESSING') {

                return [
                    'code' => 0,
                    'data' => $responseData,
                ];
            } else {
                return [
                    'code' => 1,
                    'data' => $responseData,
                ];
            }
        } else {
            $error = curl_error($ch);
            return [
                'code' => 1,
                'data' => $error,
            ];
        }
        curl_close($ch);
    }
    
    /*
     * 微信退款(小程序)
     */
    public static function XcxWxRefund($out_refund_no, $total_fee,$refund_fee)
    {

//        $sslcert_path = dirname(realpath(__FILE__)) . "\\cert\\apiclient_cert.pem";
//        $sslkey_path = dirname(realpath(__FILE__)) . "\\cert\\apiclient_key.pem";
        
        $sslcert_path = ROOT_PATH . "application/util/xcxcert/apiclient_cert.pem";
        $sslkey_path = ROOT_PATH . "application/util/xcxcert/apiclient_key.pem";

        $data['appid'] = "wxf03b6b5aaedfa785";
        $data['mch_id'] = '1355332802';
        $data['nonce_str'] = self::generateNonceStr();
        $data['sign_type'] = 'MD5';
        $data['out_trade_no'] = $out_refund_no;
        $data['out_refund_no'] = $out_refund_no;
        $data['total_fee'] = $total_fee * 100;  //订单金额为分
        $data['refund_fee'] = $refund_fee * 100; //退款金额为分
        $data['refund_fee_type'] = 'CNY';
        //$data['refund_account'] = 'REFUND_SOURCE_RECHARGE_FUNDS';
        $data['notify_url'] = self::XCX_REFUND_NOTIFY_URL;
        $data['sign'] = self::xcxsign($data);

        $xml = self::arrayToXml($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::REFUND_CURL_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //ssl证书
        curl_setopt($ch,CURLOPT_SSLCERT, $sslcert_path);
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, $sslkey_path);

        $result = curl_exec($ch);

        if (!curl_errno($ch)) {
            $responseData = self::xmlToArray($result);
            if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
                return [
                    'code' => 0,
                    'data' => $responseData,
                ];
            } else {
                return [
                    'code' => 1,
                    'data' => $responseData,
                ];
            }
        } else {
            $error = curl_error($ch);
            return [
                'code' => 2,
                'data' => $error,
            ];
        }
        curl_close($ch);
    }

}